package com.demo.javaDemo.nio;

import java.io.IOException;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class UrlChannelDemo {

	public static void main(String[] args) throws IOException {
		SocketChannel socketChannel = SocketChannel.open();
		URL url=Thread.currentThread().getContextClassLoader().getResource("data/nio-data.txt");
//		InetSocketAddress net=new InetSocketAddress("ifeve.com", 80);
//		boolean flag=socketChannel.connect(net);
//		System.out.println("net connet is ok? "+flag);
//		Thread.currentThread().getContextClassLoader().getResourceAsStream("data/nio-data.txt").readAllBytes();
		ByteBuffer buf = ByteBuffer.allocate(48);
		StringWriter wt=new StringWriter();
		int bytesRead = socketChannel.read(buf);
		while (bytesRead != -1) {
			System.out.println("Read " + bytesRead);
			buf.flip();
			while (buf.hasRemaining()) {
				wt.append((char) buf.get());
			}
			buf.clear();
			bytesRead = socketChannel.read(buf);
		}
		System.out.println(wt.getBuffer().toString());

	}

}
